
    /**  
    * @Title: DefaultDateSourceConfig.java
    * @Package com.efast.cafe.framework.configuration
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年9月14日
    * @version V1.0  
    */
    
package com.efast.cafe.framework.datasource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.sql.SQLException;

import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.efast.cafe.framework.properties.DruidDefaultDataSourceConfigProperties;



/**
    * @ClassName: DefaultDateSourceConfig
    * @Description: 默认数据源以及事务管理器配置配置
    * @author Administrator
    * @date 2018年9月14日
    *
    */
@ConditionalOnProperty(
        name="cafe.datasource.multi",
        havingValue="false"
)
@Configuration
@AutoConfigureAfter(DruidDefaultDataSourceConfigProperties.class)
public class DefaultDateSourceConfig {
	
	
	@Autowired
    private DruidDefaultDataSourceConfigProperties druidDefaultDataSourceConfigProperties;
	
	@Primary
	@Bean("defaultDataSource")
    public DataSource defaultDataSource(){
		DruidDataSource datasource = new DruidDataSource();
        /* 基础配置 */
        datasource.setUrl(this.druidDefaultDataSourceConfigProperties.getUrl());
        datasource.setUsername(this.druidDefaultDataSourceConfigProperties.getUsername());
        datasource.setPassword(this.druidDefaultDataSourceConfigProperties.getPassword());
        datasource.setDriverClassName(this.druidDefaultDataSourceConfigProperties.getDriverClassName());
        try {
        	if(druidDefaultDataSourceConfigProperties.getFilters()!=null)
        		datasource.setFilters(druidDefaultDataSourceConfigProperties.getFilters());
		} catch (SQLException e) {
			e.printStackTrace();
		}

        /* 其他配置 */
        datasource.setInitialSize(this.druidDefaultDataSourceConfigProperties.getInitialSize());
        datasource.setMinIdle(this.druidDefaultDataSourceConfigProperties.getMinIdle());
        datasource.setMaxActive(this.druidDefaultDataSourceConfigProperties.getMaxActive());
        datasource.setMaxWait(this.druidDefaultDataSourceConfigProperties.getMaxWait());
        datasource.setTimeBetweenEvictionRunsMillis(this.druidDefaultDataSourceConfigProperties.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(this.druidDefaultDataSourceConfigProperties.getMinEvictableIdleTimeMillis());
        datasource.setValidationQuery(this.druidDefaultDataSourceConfigProperties.getValidationQuery());
        datasource.setTestWhileIdle(this.druidDefaultDataSourceConfigProperties.getTestWhileIdle());
        datasource.setTestOnBorrow(this.druidDefaultDataSourceConfigProperties.getTestOnBorrow());
        datasource.setTestOnReturn(this.druidDefaultDataSourceConfigProperties.getTestOnReturn());

        return datasource;
    }
	
}
